<template>
  <div class="app-container">
    <el-form ref="form" :model="formData" :label-width="labelWidth" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="名称" />
      </el-form-item>
      <el-form-item label="关联阀门">
        <el-transfer
          v-model="formData.ports"
          filterable
          :filter-method="filterMethod"
          filter-placeholder="请输入阀控名称"
          :props="valveProps"
          :data="extraData"
          :titles="['可选阀控', '已选阀控']"
        />
      </el-form-item>
      <el-form-item :label-width="labelWidth">
        <el-button type="primary" @click="submit('form')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    serialno: {
      type: String,
      default() {
        return ''
      }
    },
    formData: {
      type: Object,
      default() {
        return {
          name: '',
          ports: []
        }
      }
    },
    extraData: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      labelWidth: '80px',
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      valveProps: {
        key: 'idx',
        label: 'name',
        disabled: 'disabled'
      }
    }
  },
  methods: {
    filterMethod(query, item) {
      return item.name.indexOf(query) > -1
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submit', this.formData)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
